package com.prashantdctm.tsms;

import java.io.BufferedReader;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.gargoylesoftware.htmlunit.gae.GAEUtils;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.prashantdctm.paidemail.GaeHttpServlet;
import com.prashantdctm.utils.EmailUtil;
import com.prashantdctm.utils.GaeUtil;
import com.prashantdctm.utils.GeneralUtil;
import com.prashantdctm.utils.HttpUtil;
import com.prashantdctm.utils.XMLUtil;

public class TSMSServlet extends GaeHttpServlet {
	private static final long serialVersionUID = 1L;
	public static boolean debugMode = false;
	
	public static void main(String[] args) throws Exception {
		webClient = HttpUtil.getWebClient();
		initLocalLog("C:\\Users\\prashantkumar\\logs", true);
		updateTsms("http://myhcl.com/Login", "prashantkumar", "Uandi!@90", "HCLTECH");
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		super.doGet(req, resp);
		
		try {
			updateTsms(req.getParameter("site"), req.getParameter("user"), req.getParameter("pwd"), req.getParameter("domain"));
		} catch (Exception e) {
			customException("Error in updating TSMS", e);
		}
		resetMessageLog();
		if (GAEUtils.isGaeMode()) {
			EmailUtil.sendEmail(messageLog.toString(), "GAE Report");
		}
	}
	
	public static void sendGAEReport(){
		GaeUtil.sendReport();
	}
	
	public static void updateTsms(String site, String username, String pwd, String domain) throws Exception {
		HtmlPage page = null;
		if(site == null || username == null || pwd == null) {
			customLog("Site or user or password not supplied");
			return;
		}
		customLog("Logging in "+site+" as " + username);
		
		try {
			page = webClient.getPage(site);
			((HtmlTextInput) page.getElementByName("txtUserID")).setValueAttribute(username);
			((HtmlPasswordInput) page.getElementByName("txtPassword")).setValueAttribute(pwd);
			HtmlSelect type = (HtmlSelect) page.getElementByName("ddlDomain");
			List<HtmlOption> options = type.getOptions();
            for(HtmlOption option: options) {                    
            	if(option.getText().equals(domain) ) {
                      type.setSelectedAttribute(option, true);
                      break;
                }
            }
            HtmlElement loginElement = page.getElementById("btnSubmit");
            page = loginElement.click();
		} catch (Exception e) {
			customException("Error in logging ", e);
			return;
		}
		
		GeneralUtil.dumpPage(page);
		//Time sheet reminder page
		try {
			HtmlElement element  = ((HtmlElement) page.getByXPath("//input[@value='Go to Time Sheet']").get(0));
			if(element!=null) {
				page = element.click();
			}
		} catch (Exception e) {}
		
		//Go to direct entry page
		page = webClient.getPage("http://wf4.myhcl.com/TSMS/TSMS_HCLT/TH_EnterTimeSheetNew.aspx");
		List<HtmlTextInput> txtInputs  = (List<HtmlTextInput>) page.getByXPath("//input[@type='text']");
		
		for(HtmlTextInput txt : txtInputs) {
			if(txt.getNameAttribute().startsWith("txtHoursRow0Day") && txt.getValueAttribute().equals("")) {
				txt.setValueAttribute("09:00");
			}
		}
		page.getElementByName("btnSave").click();
		
		webClient.closeAllWindows();
	}	
}
